import UserPwdRules from '../modules/rules/base_rules'

export default {
    data() {
        return {
            extModules: {
                pwdModule: {
                    pwdModal: false,
                    pwdLoading: false,
                    pwdForm: {
                        username: '',
                        newPassword: '',
                        newPassword2: ''
                    },
                    rules: UserPwdRules
                }
            }
        }
    },
    methods: {
        onEditPwd(index){
            let row = this.getRow(index)
            this.extModules.pwdModule.pwdForm.username = row.username
            this.extModules.pwdModule.pwdModal = true
        },
        onCancelPwdChange(){
            this.extModules.pwdModule.pwdModal = false
        },
        onPwdChange(){
            if(this.valideForm('pwdForm')){
                var module = this.extModules.pwdModule
                var form = module.pwdForm
                if(form.newPassword !== form.newPassword2){
                    this.$Message.error('两次密码必须相同')
                    return
                }
                module.pwdLoading = true
                this.$http.put(`${this.moduleRout}/update_password`, form).then((resp) => {
                    this.$Message.success('密码修改成功')
                    form.newPassword = form.newPassword2 = ''
                    module.pwdLoading = false
                    module.pwdModal = false
                }).catch((err) => {
                    console.warn('修改密码失败：', err)
                    module.pwdLoading = false
                })
            }
        }
    }
}
